package com.app.qbake.util;

import android.content.Context;
import android.content.Intent;

import com.app.qbake.base.PhotoPreviewActivity;
import com.app.qbake.base.bean.PhotoPreview;
import com.wind.base.C;
import com.wind.data.square.bean.Pic;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

import static com.wind.base.C.Value.REQUEST_CODE_CAMERA;
import static com.wind.base.C.Value.REQUEST_CODE_GALLERY;

/**
 * Created by wind on 2017/3/22.
 */

public class GalleryHelper {

    /**
     * 预览照片
     *
     * @param context
     * @param photos
     * @param position
     */
    public static void preview(final Context context, List<Pic> photos, final int position) {
        Observable.from(photos)/*.filter(new Func1<UploadPhoto, Boolean>() {
            @Override
            public Boolean call(UploadPhoto uploadPhoto) {
                return !TextUtils.isEmpty(uploadPhoto.getPath());
            }
        })*/
                .map(new Func1<Pic, PhotoPreview>() {
                    @Override
                    public PhotoPreview call(Pic uploadPhoto) {
                        PhotoPreview preview = new PhotoPreview();
                       // preview.setExtra(uploadPhoto.get());
                        preview.setPath(uploadPhoto.getUrl());
                        //preview.setResId(uploadPhoto.getResId());
                        return preview;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<PhotoPreview>() {
                    private ArrayList<PhotoPreview> previews = new ArrayList<>();

                    @Override
                    public void onCompleted() {
                        Intent intent = new Intent(context, PhotoPreviewActivity.class);

                        intent.putExtra(PhotoPreviewActivity.EXTRA_KEY_PHOTOS, previews);
                        intent.putExtra(PhotoPreviewActivity.EXTRA_KEY_START_POSITION, position);

                        /*Pair pair1 = Pair.create(shareElement, ViewCompat.getTransitionName(shareElement));
                        ActivityOptionsCompat options = ActivityOptionsCompat
                                .makeSceneTransitionAnimation(getActivity(), pair1);
                        ActivityCompat.startActivity(getActivity(), intent, options.toBundle());*/

                        context.startActivity(intent);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(PhotoPreview photoPreview) {
                        previews.add(photoPreview);
                    }
                });
    }


    /**
     * 多选相册
     */
    public static void multiOpenGallery(int maxPhotoCount,
                                        GalleryFinal.OnHanlderResultCallback callback) {

        //带配置
        FunctionConfig config = new FunctionConfig.Builder()
                .setMutiSelectMaxSize(maxPhotoCount)
                .build();
        GalleryFinal.openGalleryMuti(REQUEST_CODE_GALLERY, config, callback);
    }


    public static void openCamera(boolean crop, GalleryFinal.OnHanlderResultCallback callback) {
        if (!crop) {
            FunctionConfig config = new FunctionConfig.Builder()
                    .setEnableCrop(false)
                    .setEnableEdit(false)
                    .build();
            GalleryFinal.openCamera(REQUEST_CODE_CAMERA, config, callback);
        } else {
            GalleryFinal.openCamera(REQUEST_CODE_CAMERA, callback);
        }
    }


    public static void singleOpenGallery(GalleryFinal.OnHanlderResultCallback callback) {
        GalleryFinal.openGallerySingle(REQUEST_CODE_GALLERY, callback);
    }
    public static void singleOpenGallery(boolean crop,GalleryFinal.OnHanlderResultCallback callback) {
        if (!crop) {
            FunctionConfig config = new FunctionConfig.Builder()
                    .setEnableCrop(false)
                    .setEnableEdit(false)
                    .build();
            GalleryFinal.openGallerySingle(REQUEST_CODE_CAMERA, config, callback);
        } else {
            GalleryFinal.openGallerySingle(REQUEST_CODE_CAMERA, callback);
        }
    }

    public static int getMaxPhotoCount(int photoItemCount,boolean containsAddPhoto) {
            int count= C.Value.MAX_PHOTO_COUNT;
            if (containsAddPhoto){
                count=count-photoItemCount+1;
            }else {
                count-=photoItemCount;
            }
            return count;
    }


}
